package com.erpanderp.wam.usm.model.service.user;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.usm.model.entity.user.BirthDayWishes;
import com.erpanderp.wam.util.HibernateUtil;

public class BirthDayWishesDAO {
	
	public BirthDayWishes saveOrUpdateBirthDayWishes(BirthDayWishes birthDayWishes){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.saveOrUpdate(birthDayWishes);
		session.getTransaction().commit();
		return birthDayWishes;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public List<BirthDayWishes> getAllEmpBdaysByDate(){
		List<BirthDayWishes> birthDayWishes = new ArrayList<BirthDayWishes>();
	
		Calendar calendar = Calendar.getInstance();		
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);  
		int month = calendar.get(Calendar.MONTH)+1; 
		int year=calendar.get(Calendar.YEAR); 
	  try{			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from BirthDayWishes bw where MONTH(bw.sentOn)=:month and DAY(bw.sentOn)=:dayOfMonth and YEAR(bw.sentOn)=:year";
			Query query = session.createQuery(queryString).setInteger("dayOfMonth", dayOfMonth).setInteger("month", month).setInteger("year", year);
			query.setCacheable(false);
			birthDayWishes =(List<BirthDayWishes>) query.list();
			session.getTransaction().commit();
			return birthDayWishes;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public BirthDayWishes getBirthDayWishesById(Long birthDayWishesId){
		BirthDayWishes birthDayWishes = null;
		try
		{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from BirthDayWishes bw where bw.birthDayWishesId=:birthDayWishesId";
			Query query = session.createQuery(queryString).setLong("birthDayWishesId", birthDayWishesId);
			birthDayWishes = (BirthDayWishes) query.uniqueResult();
			session.getTransaction().commit();
			return birthDayWishes;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public static void main(String a[]){
		System.out.println(new BirthDayWishesDAO().getAllEmpBdaysByDate());
		//new EmployeeDAO().getCompetencyLeads();
	}
	


}
